/*
 * MessageReceiveIHM.java
 *
 * Created on 25 févr. 2011, 22:09:56
 */

package com.darrylsite.ihm;

import com.darrylsite.listener.QueueListener;
import com.darrylsite.core.MyEntityManager;
import com.darrylsite.entite.Message;
import com.darrylsite.entite.SentMessages;
import com.darrylsite.ihm.reader.sentMenu;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * interface affichant les messages envoyes
 * @author nabster
 */
public class MessageSentIHM extends javax.swing.JPanel implements QueueListener
{
     List<Message> messages;
     MessageSentIHM _self = this;
     sentMenu popup;

    /** Creates new form MessageReceiveIHM */
    public MessageSentIHM()
    {
        initComponents();
        init();
    }

     private void init()
    {
        msgViewer = new MessageViewer();
     this.add(msgViewer, BorderLayout.SOUTH);
        
     popup = new sentMenu();
     popup.setTable(msgDsp.getTable());
     this.msgDsp.addMouseListener(new MouseAdapter(){

            @Override
            public void mouseClicked(MouseEvent e)
            {
                int t = msgDsp.getTable().getSelectedRow();
                if(t>-1)
                    msgViewer.setData(messages.get(t));
                
                if(e.getClickCount()!=2)
                    return;
                popup.setLocation(e.getLocationOnScreen());
                popup.show(_self, e.getPoint().x, e.getPoint().y);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        msgDsp = new com.darrylsite.ihm.MessageDisplayer();

        setLayout(new java.awt.BorderLayout());
        add(msgDsp, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.darrylsite.ihm.MessageDisplayer msgDsp;
    // End of variables declaration//GEN-END:variables
    private MessageViewer msgViewer;

    @Override
    public void change()
    {
      EntityManager em = MyEntityManager.emf.createEntityManager();
      Query query = em.createQuery("select m from SentMessages m");
      List liste = query.getResultList();
      if(liste.isEmpty())
          return;
      SentMessages ms = (SentMessages) liste.get(0);
      messages = ms.getMessages();
      popup.setMessages(messages);
      
      Object[][] data = new Object[messages.size()][4];
      int i=0;
      for(Message m : messages)
      {
       data[i][0] = m.getNumero();
       data[i][1] = m.getMessage();
       data[i][2] = m.getDates();
       data[i][3] = m.getStatus();
       i++;
      }
      popup.setVisible(false);
      this.msgDsp.refreshData(data);
    }

}
